﻿using B.S.InOut.Data;
using B.S.InOut.Data.OutManage;
using B.S.InOut.Data.Tranmanage;
using B.S.InOut.Domain;
using B.S.InOut.Read.Api.Application.Command;
using B.S.InOut.Read.API.Application.Command;
using MediatR;

namespace B.S.InOut.Read.Api.Application.CommandHandler
{
    // 插入移库记录的命令处理程序类
    public class InsertTranManagementCommandHandler : IRequestHandler<InsertTranManagementCommand, int>
    {
        private readonly ITranmangeRepository _repository;

        public InsertTranManagementCommandHandler(ITranmangeRepository repository)
        {
            _repository = repository;
        }

        public async Task<int> Handle(InsertTranManagementCommand request, CancellationToken cancellationToken)
        {
            return await _repository.InsertTranMenageAsync(request.Product);
        }
    }

    /// <summary>
    /// 根据 ID 获取移库记录的查询处理程序类
    /// </summary>
    public class GetTranManagementByIdQueryHandler : IRequestHandler<GetTranManagementByIdQuery, Tranmanagement>
    {
        private readonly ITranmangeRepository _repository;

        public GetTranManagementByIdQueryHandler(ITranmangeRepository repository)
        {
            _repository = repository;
        }

        public async Task<Tranmanagement> Handle(GetTranManagementByIdQuery request, CancellationToken cancellationToken)
        {
            return await _repository.GetTranMenageByIdAsync(request.Id);
        }
    }


    public class GetTranManagementAllQueryHandler : IRequestHandler<GetTranManagementAllQuery, IEnumerable<Tranmanagement>>
    {
        private readonly ITranmangeRepository _repository;

        public GetTranManagementAllQueryHandler(ITranmangeRepository repository)
        {
            _repository = repository;
        }

        public async Task<IEnumerable<Tranmanagement>> Handle(GetTranManagementAllQuery request, CancellationToken cancellationToken)
        {
            return await _repository.GetTranMenageByConditionsAsync(request.TransferNum, request.Transfer, request.TranAuditstatus, request.pageindex, request.pagesize);
        }
    }

    public class DeleteTranmanageCommandHandler : IRequestHandler<DeleteTranmanageCommand, int>
    {
        // 出库信息仓储实例
        private readonly ITranmangeRepository _repository;

        public DeleteTranmanageCommandHandler(ITranmangeRepository repository)
        {
            _repository = repository;
        }

        // 构造函数，注入出库信息仓储实例
        public async Task<int> Handle(DeleteTranmanageCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的删除方法
            return await _repository.DeleteTranMenageAsync(request.Id);
        }
    }

    /// <summary>
    /// 批量删除产品命令处理程序
    /// </summary>
    public class DeleteALLTranmanageCommandHandler : IRequestHandler<DeleteTranmanageCommandALL, int>
    {
        private readonly ITranmangeRepository _repository;

        /// <summary>
        /// 构造函数，注入产品仓储实例
        /// </summary>
        /// <param name="repository">产品仓储实例</param>
        public DeleteALLTranmanageCommandHandler(ITranmangeRepository repository)
        {
            _repository = repository;
        }
        public async Task<int> Handle(DeleteTranmanageCommandALL request, CancellationToken cancellationToken)
        {
            return await _repository.DeleteTranMenageAsyncd(request.ProductIds);

        }
    }

    // 更新产品命令处理程序类，实现 IRequestHandler 接口，处理更新产品命令
    public class UpdateTranmanageCommandHandler : IRequestHandler<UpdateTranmanageCommand, int>
    {
        // 产品仓储实例
        private readonly ITranmangeRepository _repository;

        // 构造函数，注入产品仓储实例
        public UpdateTranmanageCommandHandler(ITranmangeRepository repository)
        {
            _repository = repository;
        }

        // 处理更新产品命令的异步方法
        public async Task<int> Handle(UpdateTranmanageCommand request, CancellationToken cancellationToken)
        {
            return await _repository.UpdateTranMenageAsync(request.Product);
        }
    }
}
